package com.zhiyou.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.mapper.VideoMapper;
import com.zhiyou.pojo.QueryVideoVo;
import com.zhiyou.pojo.Video;
import com.zhiyou.pojo.VideoExample;
import com.zhiyou.pojo.VideoExample.Criteria;

@Service
public class VideoService {

	@Autowired
	private VideoMapper videoMapper;

	public List<Video> selectAll() {

		return videoMapper.selectByExampleWithBLOBs(new VideoExample());
	}

	public List<Video> selectVideoByQueryVo(QueryVideoVo video) {

		return videoMapper.selVideoListByQuVo(video);
		//------>ȥvideoMapper�ӿ���д���󷽷�
		//ȥvideoMapper.xml��дSQL
	}
	public int countVideoByQueryVo(QueryVideoVo video) {

		return videoMapper.countVideoByQueryVo(video);
		
	}

	public void saveOrUpdate(Video video) {

		if (null != video.getId() && video.getId() != 0) {
			videoMapper.updateByPrimaryKeyWithBLOBs(video);
		} else {
			video.setPlayNum(0);
			videoMapper.insert(video);
		}
	}

	/*
	 * ����ɾ����ݵķ��� ��һ����VideoService����д���� �ڶ�����VideoController����service��ķ���
	 */
	public int deletBacthVideoById(Integer[] ids) {
		VideoExample example = new VideoExample();
		// ctrl + 1 �Զ�����ֶ�
		Criteria criteria = example.createCriteria();

		// ����תΪ����
		List<Integer> list = new ArrayList<Integer>();
		Collections.addAll(list, ids);
		// delete from video where id in(1,2,3,5);
		criteria.andIdIn(list);
		return videoMapper.deleteByExample(example);

	}

	public int delById(int id) {
		return videoMapper.deleteByPrimaryKey(id);
	}

	public Video selectById(int id) {

		return videoMapper.selectByPrimaryKey(id);
	}

	public List<Video> selectByCourseId(int courseId) {

		VideoExample example=new VideoExample();
		Criteria criteria = example.createCriteria();
		criteria.andCourseIdEqualTo(courseId);
		
		return videoMapper.selectByExampleWithBLOBs(example);
	}

}





























